package com.example.shop.fgo.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shop.fgo.Constant;
import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.CreateDirEntity;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.net.CreateDirServe;
import com.example.shop.fgo.ui.fragment.WPFragment;
import com.example.shop.fgo.utils.CookieUtil;
import com.example.shop.library.net.NetJsonUtil;
import com.example.shop.library.util.UtilEdt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/2/17.
 */

public class DigCreateDir extends Dialog{
    private EditText mEdtContent;
    private Button mBtConfirm;
    private Button mBtCancle;
    private WPFragment mWPFragment;
    public DigCreateDir(final Context context, WPFragment wpFragment) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mWPFragment=wpFragment;
        setContentView(R.layout.dig_create_dir);
        mEdtContent = (EditText) findViewById(R.id.edt_content);
        mBtConfirm = (Button) findViewById(R.id.bt_confirm);
        mBtCancle = (Button) findViewById(R.id.bt_cancle);
        mBtCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });

        mBtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content= UtilEdt.getEdtText(mEdtContent);
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(context, "文件夹名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                doNet(content);
            }

        });
    }

    private void doNet(String content) {
        //1.检查文件名是否已存在
        List<FileListEntity.DataEntity> dataEntitys = mWPFragment.mDataEntitys;
        for (FileListEntity.DataEntity dataEntity : dataEntitys) {
            if (TextUtils.isEmpty(dataEntity.getCid())) {
                break;
            }
            if (content.equals(dataEntity.getN())) {
                Toast.makeText(getContext(), "文件夹"+content+"已存在", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        //2.访问网络，创建文件夹
        CreateDirServe createDirServe = NetJsonUtil.getRetrofit(Constant.BASE_URL, CreateDirServe.class);
        Map<String,Object> params=new HashMap<>();
        params.put("cname",content);
        params.put("pid",mWPFragment.mCidNow);
        Call<CreateDirEntity> createDirEntityCall = createDirServe.requestWL(params, CookieUtil.getCookie());
        createDirEntityCall.enqueue(new Callback<CreateDirEntity>() {
            @Override
            public void onResponse(Call<CreateDirEntity> call, Response<CreateDirEntity> response) {
                CreateDirEntity body = response.body();
                if (body.isState()) {
                    Toast.makeText(getContext(), "创建成功", Toast.LENGTH_SHORT).show();
                    cancel();
                    mWPFragment.mLayoutRefresh.setRefreshing(true);
                }else{
                    Toast.makeText(getContext(), "创建失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CreateDirEntity> call, Throwable t) {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
